﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;

namespace RN.ERP.Web.UserControl
{
    public partial class UCMarketInfo : RN.ERP.Web.Utilities.BaseUserControl
    {
        public string CorpUserGuid { get; set; }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            InitControl();
        }

        private void BindData()
        {
            Guid _corpUserGuid = Guid.Empty;
            string sysArea=ConfigurationManager.AppSettings["sysArea"];
            if (Guid.TryParse(CorpUserGuid, out _corpUserGuid))
            {
                QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = _corpUserGuid, O = OptEnum.Equal } };
                List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
                queryCompanyInfoList.Add(queryCompanyInfoCondition);
                var jcorp = enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), sysArea);
                var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(jcorp);
                if (corpInfo == null)
                {
                    return;
                }
                var flow = JsonConvert.DeserializeObject<Tb_FlowCode>(enterpriseServiceClient.GetCorpFlowCode(JsonConvert.SerializeObject(corpInfo), UserInfo.CurrentArea));
                string flowCode = string.Empty;
                if (flow != null)
                {
                    flowCode = string.IsNullOrEmpty(flow.FlowCode) ? "" : flow.FlowCode;
                }
                
                if (string.IsNullOrEmpty(sysArea))
                {
                    sysArea = UserInfo.CurrentArea;
                }
                else
                {
                    hdnSysArea.Value = sysArea;
                }
                if (string.IsNullOrEmpty(sysArea))
                {
                    ltrCurrentMarket.Text = "未添加";
                }
                else
                {
                    ltrCurrentMarket.Text = GetAreaTitle(sysArea);
                }
                //ltrCurrentMarketFlow.Text = basicServiceClient.GetFlowCodeTitle(sysArea, corpInfo.classID.ToString(), flowCode);
            }
            
        }

        private void InitControl()
        {
            if (UserInfo.RoleType.ToLower() != "user")
            {
                //divCurrentFlowCode.Visible = false;
                divCurrentMark.Visible = false;
            }
            ////企业端可以查看其他企业信息，该状态下不允许进行市场切换
            if (CorpUserGuid != UserInfo.CorpUserGuid)
            {
                divCurrentMark.Visible = false;
            }
        }

        private string GetAreaTitle(object areaCode)
        {
            string title = "";
            if (areaCode != null)
            {
                string code = areaCode.ToString();
                if (code.Length >= 2)
                {
                    var areaInfos = JsonConvert.DeserializeObject<List<a_AreaInfo>>(Application[ApplicationConstant.APPLICATION_AREA].ToString());
                    var currentArea = areaInfos.Where(a => code.StartsWith(a.Code)).FirstOrDefault();
                    title = currentArea == null ? "" : currentArea.Title;
                }
            }
            return title;
        }

        
    }
}